package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.system.domain.PwsStatus;

import com.ruoyi.system.domain.PwsTable;
import com.ruoyi.system.domain.SapData;
import com.ruoyi.system.domain.TankInfo;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.PwsStatusMapper;

import com.ruoyi.system.service.PwsStatusService;

import com.ruoyi.system.service.PwsTableService;
import com.ruoyi.system.service.SapDataService;
import com.ruoyi.system.service.TankInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * pws 服务实现类
 * </p>
 *
 * @author tianfu-li
 * @since 2025-44-27
 */
@Service
public class PwsStatusServiceImpl extends ServiceImpl<PwsStatusMapper, PwsStatus> implements PwsStatusService {

    @Autowired
    private PwsStatusMapper pwsStatusMapper;
    @Autowired
    TankInfoService tankInfoService;
    @Autowired
    SapDataService sapDataService;
    @Autowired
    PwsTableService pwsTableService;

    @Override
    public List<ProductionStatusVo> getProductData() {
        //未生产
        ProductionStatusVo<AlreadyProductVo> productionStatusAlreadyProductVo = new ProductionStatusVo<>();
        List<AlreadyProductVo> AlreadyProductVoList= pwsStatusMapper.getAlreadyProductData();
        productionStatusAlreadyProductVo.setTitle("已生产");
        productionStatusAlreadyProductVo.setNumbers(AlreadyProductVoList);

        //未生产
        ProductionStatusVo<NoProductVo> productionStatusNoProductVo = new ProductionStatusVo<>();
        List<NoProductVo> NoProductVoList= pwsStatusMapper.getNoProductData();
        productionStatusNoProductVo.setTitle("未生产");
        productionStatusNoProductVo.setNumbers(NoProductVoList);

        //正在生产
        ProductionStatusVo<InProductVo> productionStatusInProductVo = new ProductionStatusVo<>();
        List<InProductVo> inProductVoList= pwsStatusMapper.getInProductData();
        productionStatusInProductVo.setTitle("正在生产");
        productionStatusInProductVo.setNumbers(inProductVoList);

        //整合
       List<ProductionStatusVo> productionStatusVoList = new ArrayList<>();
        productionStatusVoList.add(productionStatusAlreadyProductVo);
        productionStatusVoList.add(productionStatusNoProductVo);
        productionStatusVoList.add(productionStatusInProductVo);
        return productionStatusVoList;
    }

    @Override
    public TankAndPwsAndSapVo getTankAndPwsAndSapData(Long lineNo)  {
        TankAndPwsAndSapVo tankAndPwsAndSapVo = new TankAndPwsAndSapVo();
        TankInfo tankInfo = tankInfoService.getTankInfoByLineNo(lineNo).get(0);
        tankAndPwsAndSapVo.setTankInfo(tankInfo);
        PwsTable pwsTable = pwsTableService.getPwsTableByOrderNo(tankInfo.getOrderNo());
        tankAndPwsAndSapVo.setPwsTable(pwsTable);
        PwsStatus pwsStatus = this.selectByOrderNo(tankInfo.getOrderNo());
        tankAndPwsAndSapVo.setPwsStatus(pwsStatus);
        List<SapData> sapDataList = sapDataService.selectByOrderNo(tankInfo.getOrderNo());
        tankAndPwsAndSapVo.setSapDataList(sapDataList);
        return tankAndPwsAndSapVo;
    }

    @Override
    public List<PwsStatus> selectByOrderNos(List<String> orderNos) {
        LambdaQueryWrapper<PwsStatus> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PwsStatus::getOrderNo,orderNos);
        wrapper.eq(PwsStatus::getIsDel,0);
        wrapper.eq(PwsStatus::getFuheStatus,1);
        return baseMapper.selectList(wrapper);

    }

    @Override
    public PwsStatus getPwsStatusByOrderNo(String orderNo) {
        LambdaQueryWrapper<PwsStatus> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PwsStatus::getOrderNo,orderNo);
        wrapper.eq(PwsStatus::getIsDel,0);
        //wrapper.eq(PwsStatus::getFuheStatus,1);
        return baseMapper.selectOne(wrapper);
    }

    public PwsStatus selectByOrderNo(String orderNo) {
        LambdaQueryWrapper<PwsStatus> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PwsStatus::getOrderNo, orderNo);
        wrapper.eq(PwsStatus::getIsDel,0);
        return baseMapper.selectOne(wrapper);

    }

    @Override
    public PwsStatus selectPwsStatusByOrderNo(String orderNo, int status) {
        LambdaQueryWrapper<PwsStatus> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PwsStatus::getOrderNo, orderNo);
        wrapper.eq(PwsStatus::getIsDel,0);
        wrapper.eq(PwsStatus::getIsReady,status);
        return baseMapper.selectOne(wrapper);
    }

    public  boolean updatePwsStatus(PwsStatus pwsStatus){
        return baseMapper.updateById(pwsStatus)>0;
    }
}
